package servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//@WebFilter("/servletB")
public class MyFilterA implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("filter-init");
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 处理过滤逻辑
        System.out.println("doFilter-before");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("doFilter-after");

//        // 登录过滤
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpSession session = request.getSession();
//        String session1 = (String) session.getAttribute("session1");
//        System.out.println("session1" + session1);
//        if (session1 != null) {
//            // 用户已经登录
//            // 放行
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else {
//            request.getRequestDispatcher("index.jsp").forward(servletRequest, servletResponse);
//        }
    }

    @Override
    public void destroy() {
        System.out.println("filter-destroy");
        Filter.super.destroy();
    }
}
